import {
  queryAdminList,
  editPower,
  queryUserList,
  deleteUserById,
  modifyUserInfo,
  saveNewUser,
  modifyPassword
} from '@/api/manage'
import log from 'echarts/src/scale/Log'
import { da } from 'pinyin/data/dict-zi-web'

const state = {
  adminList: [],
  userList: []
}

const mutations = {

  /**
   * 保存所有管理员信息到Vuex中
   * @param {*} state
   * @param {*} data
   */
  SET_ADMINLIST: (state, data) => {
    console.log(data)
    state.adminList = data.map((admin) => {
      return { ...admin, avatar: window.application.minio_host + admin.avatar }
    })
  },

  /**
   * 保存用户信息
   * @param {*} state
   * @param {*} data
   */
  SET_USERLIST: (state, data) => {
    data.records = data.records.map((user) => {
      return { ...user, user_image: window.application.minio_host + user.user_image }
    })
    state.userList = data
  }

}

const actions = {

  /**
   * 获取所有管理员信息
   * @param {*} param0
   * @returns
   */
  queryAdminList({ commit }) {
    return new Promise((resolve, reject) => {
      queryAdminList().then(
        resp => {
          const { data } = resp.data
          // sessionStorage.setItem('adminList',JSON.stringify(data))
          commit('SET_ADMINLIST', data)
          resolve(resp.data)
        }).catch(
        error => {
          reject(error)
        })
    })
  },

  /**
   * 获取用户列表
   * @param {*} param0
   * @returns
   */
  queryUserList({ commit }, data) {
    return new Promise((resolve, reject) => {
      queryUserList(data).then(
        resp => {
          const { data } = resp.data
          // sessionStorage.setItem('userList',JSON.stringify(data))
          commit('SET_USERLIST', data)
          resolve(data)
        }).catch(
        error => {
          reject(error)
        }
      )
    })
  },

  /**
   * 根据用户Id删除用户
   * @param {*} param0
   * @param {*} id
   * @returns
   */
  deleteUserById({ commit }, id) {
    return new Promise((resolve, reject) => {
      deleteUserById(id).then(resp => resolve(resp)).catch(error => reject(error))
    })
  },

  /**
   * 修改管理员权限
   * @param {*} param0
   * @param {*} data
   * @returns
   */
  editPower({ commit }, data) {
    return new Promise((resolve, reject) => {
      editPower(data).then(
        resp => resolve(resp.data)).catch(error => reject(error))
    })
  },

  /**
   * 修改用户信息
   * @param {*} param0
   * @param {*} data
   * @returns
   */
  modifyUserInfo({ commit }, data) {
    return new Promise((resolve, reject) => {
      modifyUserInfo(data).then(resp => resolve(resp.data)).catch(error => reject(error))
    })
  },

  /**
   * 保存新用户
   * @param {*} param0
   * @param {*} data
   * @returns
   */
  saveNewUser({ commit }, data) {
    return new Promise((resolve, reject) => {
      saveNewUser(data).then(resp => resolve(resp.data)).catch(error => reject(error))
    })
  },

  /**
   * 修改用户密码
   * @param {*} param0
   * @param {*} data
   * @returns
   */
  modifyPassword({ commit }, data) {
    return new Promise((resolve, reject) => {
      modifyPassword(data).then(resp => resolve(resp.data)).catch(error => reject(error))
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
